/**
 * 常用方法封装 请求，文件上传等
 * @author echo. 
 **/

const tui = {
	//接口地址
	interfaceUrl: function() {
    return 'https://bx.uapps.cn'
    // return 'http://127.0.0.1:8080'
  },
	toast: function(text, duration, success) {
		wx.showToast({
			title: text || "出错啦~",
			icon: success ? 'success' : 'none',
			duration: duration || 2000
		})
  },
  

  openMap: function(lat,log,name,add){
    wx.openLocation({
      latitude: Number(lat),
      longitude: Number(log),
      name:name,
      address:add,
      success(e){

      }
    })
  },

  callphone:function(phone){
    wx.makePhoneCall({
      phoneNumber: phone,
    })
  },

	modal: function(title, content, showCancel, callback, confirmColor, confirmText) {
    wx.showModal({
			title: title || '提示',
			content: content,
			showCancel: showCancel,
			cancelColor: "#555",
			confirmColor: confirmColor || "#5677fc",
			confirmText: confirmText || "确定",
			success(res) {
				if (res.confirm) {
					callback && callback(true)
				} else {
					callback && callback(false)
				}
			}
		})
	},
  copy(data,msg){
    wx.setClipboardData({
      data: data,
      success(res) {
        wx.getClipboardData({
          success(res) {
            msg && tui.toast(msg)
          }
        })
      }
    })
  },
	isAndroid: function() {
		const res = wx.getSystemInfoSync();
		return res.platform.toLocaleLowerCase() == "android"
	},
	constNum: function() {
		let time = 0;
		time = this.isAndroid() ? 300 : 0;
		return time
	},
	delayed: null,
	showLoading: function(title, mask = true) {
		wx.showLoading({
			mask: mask,
			title: title || '请稍候...'
		})
	},
	/**
	 * 请求数据处理
	 * @param string url 请求地址
	 * @param string method 请求方式
	 *  GET or POST
	 * @param {*} postData 请求参数
	 * @param bool isDelay 是否延迟显示loading
	 * @param bool isForm 数据格式
	 *  true: 'application/x-www-form-urlencoded'
	 *  false:'application/json'
	 * @param bool hideLoading 是否隐藏loading
	 *  true: 隐藏
	 *  false:显示
	 */
	request: function(url, method, postData, isDelay, isForm, hideLoading) {
		//接口请求
		let loadding = false;
		tui.delayed && wx.hideLoading();
		clearTimeout(tui.delayed);
		tui.delayed = null;
		if (!hideLoading) {
			if (isDelay) {
				tui.delayed = setTimeout(() => {
					loadding = true
					tui.showLoading()
				}, 1000)
			} else {
				loadding = true
				tui.showLoading()
			}
		}

		return new Promise((resolve, reject) => {
			wx.request({
				url: tui.interfaceUrl() + url,
				data: postData,
				header: {
					'content-type': isForm ? 'application/x-www-form-urlencoded' : 'application/json',
					'Authorization': tui.getToken()
				},
				method: method, //'GET','POST'
				dataType: 'json',
				success: (res) => {
					clearTimeout(tui.delayed)
					tui.delayed = null;
					if (loadding && !hideLoading) {
						wx.hideLoading()
					}
					resolve(res.data)
				},
				fail: (res) => {
					clearTimeout(tui.delayed)
					tui.delayed = null;
					tui.toast("网络不给力，请稍后再试~")
					reject(res)
				}
			})
		})
	},
	
	//获取token
	getToken() {
		return wx.getStorageSync("token")
  },
  

  login(callback){
    wx.login({
      success: (res) => {
        console.log(res)
        let data = {
          code:res.code
        }
        tui.request('/auth/getOpenId','POST',JSON.stringify(data),false,false,false).then((re)=>{
          if(re.code==200){
            if(re.data.openid){
              wx.setStorageSync('openid', re.data.openid)
            }
            if(re.data.token){
              wx.setStorageSync('token', re.data.token.access_token);
              callback(re.data.token.access_token);
            }
            console.log(re)
            // wx.setStorageSync('token', re.data.access_token)
          }else{
            wx.showToast({
              title: '服务器异常',
              icon:'none'
            })
          }
        })
      },
    })
  },

}

export default tui